package com.vca.admin.controller;

import com.vca.common.exception.VcaException;
import com.vca.common.model.category.Category;
import com.vca.common.page.CommonPage;
import com.vca.common.request.CategoryRequest;
import com.vca.common.request.CategorySearchRequest;
import com.vca.common.request.PageParamRequest;
import com.vca.common.request.ProductTypeRequest;
import com.vca.common.result.CommonResult;
import com.vca.common.utils.VcaUtil;
import com.vca.common.vo.CategoryTreeVo;
import com.vca.service.service.CategoryService;
import com.vca.service.service.SystemAttachmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品类型控制器
 *
 * @author li
 * @date 2022/11/21   16:20
 */
@Slf4j
@RestController("VcaProductTypeController")
@RequestMapping("api/admin/product/type")
@Api(tags = "商品类型控制器")
public class VcaProductTypeController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SystemAttachmentService systemAttachmentService;

    /**
     * 分页显示商品分类
     * @param pageParamRequest 分页参数
     * @author li
     * @since 2022-11-21
     */
    @PreAuthorize("hasAuthority('admin:product:type:list')")
    @ApiOperation(value = "分页显示商品分类")
    @GetMapping("/list")
    public CommonResult getList(Integer status, String productTypeName,@Validated PageParamRequest pageParamRequest) {
        return CommonResult.success(categoryService.getProductTypeList(status,productTypeName));
    }

    /**
     * 新增商品类型
     * @param request 新增参数
     * @author Li
     * @date 2022/11/22 09:24
     */
    @PreAuthorize("hasAuthority('admin:product:type:add')")
    @ApiOperation(value = "新增商品类型")
    @PostMapping("/add")
    public CommonResult<String> add(@RequestBody ProductTypeRequest request) {
        return categoryService.addProductType(request)?CommonResult.success():CommonResult.failed();
    }

    /**
     * 删除商品分类
     * @param id Integer
     * @author Li
     * @since 2022/11/22 09:31
     */
    @PreAuthorize("hasAuthority('admin:product:type:delete')")
    @ApiOperation(value = "删除")
    @GetMapping("/delete")
    @ApiImplicitParam(name="id", value="分类ID")
    public CommonResult<String> delete(@RequestParam(value = "id") Integer id) {
        return categoryService.deleteProudctType(id)?CommonResult.success():CommonResult.failed();
    }

    /**
     * 修改商品分类
     * @param request 修改参数
     * @author Li
     * @since 2022/11/22 09:46
     */
    @PreAuthorize("hasAuthority('admin:product:type:update')")
    @ApiOperation(value = "修改")
    @PostMapping("/update")
    public CommonResult<String> update(@RequestBody ProductTypeRequest request) {
        return categoryService.updateProductType(request)?CommonResult.success():CommonResult.failed();
    }

    /**
     * 更改分类状态
     * @param id Integer 分类id
     * @since 2020-04-16
     * @return
     */
    @PreAuthorize("hasAuthority('admin:product:type:update:status')")
    @ApiOperation(value = "更改分类状态")
    @RequestMapping(value = "/updateStatus/{id}", method = RequestMethod.GET)
    @ApiImplicitParam(name = "id", value="分类id")
    public CommonResult<Object> getByIds(@Validated @PathVariable(name = "id") Integer id) {
        if (categoryService.updateStatus(id)) {
            return CommonResult.success("修改成功");
        } else {
            return CommonResult.failed("修改失败");
        }
    }
}
